package com.wolf.cx330.view.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.wolf.cx330.base.getAvatar

/**
 * 图文混排
 */
class ImageTextView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val text =
        "Nulla a ullamcorper enim. Curabitur ac purus sit amet nunc condimentum malesuada sed non lacus. Nunc efficitur magna nec porttitor gravida. Sed et auctor ex. Vestibulum volutpat congue neque, ut gravida enim lobortis tincidunt. Sed quis sem eros. Fusce condimentum, massa ut faucibus posuere, augue diam luctus magna, laoreet finibus neque diam sed augue. In porttitor dolor ac sapien sagittis sagittis. Aliquam euismod ex justo, ut porta dolor pulvinar nec. Donec gravida laoreet tempor. Donec varius aliquet purus eget elementum.\n" +
                "\n" +
                "Fusce eu porttitor sem, ac dictum leo. Nulla augue nisl, interdum condimentum ex eget, porta ultricies lorem. Suspendisse consectetur nibh erat, ut tempus ligula mattis vitae. Nulla facilisi. Morbi scelerisque porttitor enim, vel cursus libero eleifend et. Aenean ante augue, tempor quis enim at, ornare fermentum arcu. In vehicula nulla ut sagittis lobortis. Quisque viverra sodales neque, sit amet tempor tortor. Cras sit amet sagittis risus.\n" +
                "\n" +
                "Quisque porta a lorem sed aliquam. In dignissim elit nunc. Aliquam cursus elementum nisl id rutrum. Phasellus hendrerit mauris eu tempus maximus. Nulla viverra tortor vitae tortor pulvinar euismod. Mauris in volutpat sem, a ultrices libero. Nunc ullamcorper magna ac neque imperdiet dapibus. Vestibulum luctus velit a metus semper, eu ultricies eros lacinia. Cras eu diam eget nunc tincidunt pharetra eget quis tortor. Donec a eleifend urna, quis sodales ipsum. Nullam molestie risus vel ipsum blandit molestie. Fusce eget ligula nec massa condimentum tristique ac rhoncus nibh. Aenean dictum urna urna, tempus euismod nisl vulputate in."
    private val drawText = text + text + text + text
    private val fontMetrics = Paint.FontMetrics()

    private val imageWidth = 200
    private val imageOffset = 100F

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawBitmap(
            resources.getAvatar(imageWidth),
            width - imageWidth.toFloat(),
            imageOffset,
            paint
        )
        paint.apply {
            color = Color.BLACK
            textSize = 20F
            style = Paint.Style.FILL

        }
        paint.getFontMetrics(fontMetrics)
        var count = 0
        var start = 0
        val length = drawText.length
        val fontSpacing = paint.fontSpacing
        var verticalOffset = fontSpacing

        while (start < length) {
            //  判断是否在图片范围中
            val top = verticalOffset + fontMetrics.top
            val bottom = verticalOffset + fontMetrics.bottom
            val imgTop = imageOffset
            val imgBottom = imageOffset + imageWidth
            var useWidth = if ((top > imgTop && top < imgBottom) || (bottom > imgTop && bottom < imgBottom)){
                width-imageWidth
            }else{
                width
            }
                count =
                    paint.breakText(
                        drawText,
                        start,
                        length,
                        true,
                        useWidth.toFloat(),
                        floatArrayOf()
                    )
            println("sout . start = $start count = $count")
            canvas.drawText(drawText, start, start + count, 0F, verticalOffset, paint)
            verticalOffset += fontSpacing
            start += count
        }
    }

}